define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product/index' + location.search,
                    add_url: 'product/add',
                    edit_url: 'product/edit',
                    del_url: 'product/del',
                    multi_url: 'product/multi',
                    import_url: 'product/import',
                    table: 'product',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'merchant_id', title: __('Merchant_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'category.name', title: __('Category_id'), operate: 'LIKE'},
                        {field: 'tags', title: __('Tags'), operate: 'LIKE', formatter: Table.api.formatter.flag},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'fbx.name', title: __('Fbx_id'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'product/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'product/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'product/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                var form = $("form[role=form]");
                Form.api.bindevent(form);
                fachoosetable(form);

                function fachoosetable(form) {
                    if ($(".fachoosetable", form).size() > 0) {
                        $(".fachoosetable", form).on('click', function () {
                            var that = this;
                            var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
                            var url = $(this).data("url") ? $(this).data("url") : "";
                            parent.Fast.api.open(url + "?element_id=" + $(this).attr("id") + "&multiple=" + multiple,
                                __('Choose'), {
                                    callback: function (data) {
                                        console.log(data);

                                        var button = $("#" + $(that).attr("id"));
                                        var input_id = $(button).data("input-id") ? $(button).data("input-id") : "";
                                        if (input_id && data.multiple) {
                                            var idArr = [];
                                            var inputObj = $("#" + input_id);
                                            var value = $.trim(inputObj.val());
                                            if (value !== "") {
                                                idArr.push(inputObj.val());
                                            }
                                            idArr.push(data.id)
                                            var result = idArr.join(",");

                                            inputObj.val(result).trigger("change").trigger("validate");

                                        } else {
                                            $("#" + input_id).val(data.id).trigger("change").trigger("validate");
                                            $("#" + input_id).data("selectpicker") && $("#" + input_id).selectpicker("refresh");
                                        }

                                        // var targetTr = '';
                                        // if (data.rows.length) {
                                        //     $('#target').css('display','block');
                                        //     $.each(data.rows, function (i, j) {
                                        //         var tr = `<tr data-index=${i}>
                                        //             <td style="text-align: center; vertical-align: middle; ">${j[0].id}</td>
                                        //             <td style="text-align: center; vertical-align: middle; ">${j[0].username}</td>
                                        //             <td style="text-align: center; vertical-align: middle; ">${j[0].title}</td>
                                        //         </tr>`
                                        //         targetTr += tr;
                                        //     });
                                        //     $('#targetTr').append(targetTr);
                                        // }
                                    }
                                });
                            return false;
                        });
                    }
                }
            }
        }
    };
    return Controller;
});
